//
//  MJ_TaskViewModel.m
//  UEAssistant
//
//  Created by mac on 2017/12/28.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "MJ_TaskViewModel.h"
#import "MJ_TaskListModel.h"
#import "MJ_TaskCellLayout.h"
@implementation MJ_TaskViewModel

- (void)getTaskOrderListWith:(MJTaskOrder)type parameter:(NSDictionary *)parameter success:(ResultBlock)success {
    
    NSString *baseUrl;
    switch (type) {
        case MJTaskOrderEnterpriseWait:
            baseUrl = enterpriseWaitList_connector;
            break;
            
        case MJTaskOrderEnterpriseOngoing:
            baseUrl = enterpriseOngoingList_connector;
            break;
            
        case MJTaskOrderWaitWithGroup:
            baseUrl = orderList_connector;
            break;
            
        case MJTaskOrderOngoingWithGroup:
            baseUrl = taskList_connector;
            break;
            
        case MJTaskOrderWaitWithPlat:
            baseUrl = platWaitOrderList_connector;
            break;
            
        case MJTaskOrderOngoingWithPlat:
            baseUrl = platIngOrderList_connector;
            break;
            
        case MJTaskOrderTranslateExample:
            baseUrl = @"/personal/task/filterTrail";
        default:
            break;
    }
    
    NSMutableDictionary *initParameters = [NSMutableDictionary dictionary];
    initParameters[@"pageSize"] = @"8";
    NSString *page = [NSString stringWithFormat:@"%zd",_currentPage+1];
    initParameters[@"pageNum"] = page;
    [initParameters setValuesForKeysWithDictionary:parameter];
    
    [AvailableNetWorking postWithUrl:baseUrl params:initParameters successHandler:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 200) {
            TGLog(@"%@",responseObject);
            MJ_TaskListModel *model = [MJ_TaskListModel mj_objectWithKeyValues:responseObject];
            self.pageInfoModel = model.pageInfo;
            if (_currentPage == 0) {
                [self.resultModelList removeAllObjects];
            }
            //如果总页数为0，或者总页数等于当前页，不需要再上拉刷新
            if (self.pageInfoModel.pagecount == 0 || self.pageInfoModel.pagecount == self.pageInfoModel.pagenum) {
                _hasMore = NO;
            }else{
                _hasMore = YES;
                _currentPage += 1;
            }
     
            for (MJ_TaskListResult *resultModel in model.result) {
                MJ_TaskCellLayout *layout;
                layout = [[MJ_TaskCellLayout alloc] initWithModel:resultModel type:type isHistory:NO];
                [self.resultModelList addObject:layout];
            }
            success(YES);
        }else{
            
            [TopAlertView SetUpbackgroundColor:AlertRedColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:responseObject[@"message"] andTitleColor:[UIColor whiteColor] andCancel:NO];
            success(NO);
        }
    } falureHandler:^(NSError *error) {
        success(NO);
    }];
}

- (void)getHistoryOrderListWithType:(MJTaskOrder)type parameter:(NSDictionary *)parameter success:(ResultBlock)success {
    if (type == MJTaskOrderWaitWithGroup || type == MJTaskOrderEnterpriseWait) {
        [AvailableNetWorking postWithUrl:[userManager.urlCategate stringByAppendingString:historyList_connector] params:parameter successHandler:^(id responseObject) {
            TGLog(@"history = %@",responseObject);
            [self.historyOrderList removeAllObjects];
            if ([responseObject[@"code"] intValue] == 200) {
                MJ_TaskListModel *model = [MJ_TaskListModel mj_objectWithKeyValues:responseObject];
                for (MJ_TaskListResult *resultModel in model.result) {
                    MJ_TaskCellLayout *layout = [[MJ_TaskCellLayout alloc] initWithModel:resultModel type:type isHistory:YES];
                    [self.historyOrderList addObject:layout];
                }
                [self.resultModelList addObjectsFromArray:self.historyOrderList];
                success(YES);
            }
            else{
                success(NO);
            }
        } falureHandler:^(NSError *error) {
            success(NO);
        }];
    }
}

- (NSMutableArray *)resultModelList {
    if (!_resultModelList) {
        _resultModelList = [NSMutableArray array];
    }
    return _resultModelList;
}
- (NSMutableArray *)historyOrderList {
    if (!_historyOrderList) {
        _historyOrderList = [NSMutableArray array];
    }
    return _historyOrderList;
}
- (MJ_TaskListPageInfo *)pageInfoModel {
    if (!_pageInfoModel) {
        _pageInfoModel = [[MJ_TaskListPageInfo alloc] init];
    }
    return _pageInfoModel;
}

- (void)setHasMore:(BOOL)hasMore {
    _hasMore = hasMore;
}

@end
